package com.brush.web.controller.dicttype;

import com.brush.common.annotation.Log;
import com.brush.common.core.controller.BaseController;
import com.brush.common.core.domain.AjaxResult;
import com.brush.common.core.domain.R;
import com.brush.common.core.domain.entity.SysDictType;
import com.brush.common.enums.BusinessType;
import com.brush.system.domain.req.sysdicttype.SysDictTypeAddReq;
import com.brush.system.domain.req.sysdicttype.SysDictTypeEditReq;
import com.brush.system.domain.req.sysdicttype.SysDictTypeQueryReq;
import com.brush.system.domain.resp.sysdicttype.SysDictTypeQueryResp;
import com.brush.system.service.SysDictTypeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * ClassName: SysDictType
 * Package: com.brush.web.controller.dicttype
 * Description:
 *
 * @Author luo
 * @Create 2024/8/23 19:26
 * @Version 1.0
 */

@Api(tags = "后台-字典类型api")
@RestController
@RequestMapping(value = "/admin/dict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/list")
    public R<PageInfo<SysDictTypeQueryResp>> list(SysDictTypeQueryReq req) {
        PageInfo<SysDictTypeQueryResp> page = this.sysDictTypeService.paginationQuery(req);
        return R.ok(page);
    }

    @GetMapping(value = "{id}")
    @ApiOperation(value = "根据id查询")
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    public R<SysDictTypeQueryResp> findById(@PathVariable(value = "id") Long id) {
        SysDictTypeQueryResp resp = this.sysDictTypeService.findById(id);
        return R.ok(resp);
    }

    @ApiOperation(value = "新增")
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public R add(@RequestBody @Valid SysDictTypeAddReq req) {
        boolean result = this.sysDictTypeService.add(req);
        return result ? R.ok() : R.fail();
    }

    @ApiOperation(value = "修改")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PutMapping
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    public R edit(@RequestBody @Valid SysDictTypeEditReq req) {
        boolean result = this.sysDictTypeService.edit(req);
        return super.isSuccess(result);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R remove(@PathVariable Long[] dictIds)
    {
        this.sysDictTypeService.deleteDictTypeByIds(dictIds);
        return super.isSuccess(true);
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public R refreshCache()
    {
        this.sysDictTypeService.resetDictCache();
        return R.ok();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public R optionselect() {
        List<SysDictTypeQueryResp> dictTypes = this.sysDictTypeService.optionSelect();
        return  R.ok(dictTypes);
    }

}
